
//
// 120629 - AH - (Midtun) Created.
// 120728 - AH - No longer adds Captor to Sensor.
// 120907 - AH - Handle no sensor on orb.
//

package org.soekrat.server.orbsuppliers;

import java.util.List;

import org.soekrat.engine.SoekratEngine;
import org.soekrat.engine.captor.Captor;
import org.soekrat.environment.Orb;
import org.soekrat.environment.OrbSupplier;
import org.soekrat.environment.Sensor;

import com.sun.xml.internal.ws.api.pipe.Engine;

/**
 * <p>
 *   Gets 
 *   {@link Captor}s from the
 *   {@link Engine} and give them to the corresponding
 *   {@link Sensor}.
 * </p>
 * @author Arne Halvorsen (AH)
 * @author Laetitia Le Chatton (LLC)
 */
public final class CaptorSupplier implements OrbSupplier 
{
  /**
   * <p>
   *   Constructor.
   * </p>
   */
  public CaptorSupplier(){}

  // Specified in OrbSupplier.
  public String getEngineName(){ return null; }

  // Specified in OrbSupplier.
  public String supply(Orb orb, List<OrbSupplier> secondPass) 
  {
    SoekratEngine engine=(SoekratEngine)orb.getAttribute("soekrat-engine");
    if (engine==null)
    {
      if (!secondPass.contains(this)) 
      {
        secondPass.add(this);
        return null;
      }
      
      return "CaptorSupplier: No engine";
    }
    
    Object sensorO=orb.getAttribute("sensor");
    
    if (sensorO instanceof Sensor)
    {
      Sensor sensor=(Sensor)sensorO;  
    
      String dataType=sensor.getSensedDataType();
    
      Captor[] captors=engine.getCaptors();
      for (Captor captor : captors)
      {
        String ctype=captor.getSensedDataType();
        if (ctype.equals(dataType))
        {
          //sensor.addCaptor(captor); TODO
        }
      }
    }
    
    return null;
  }
  
}
